﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm.TransferValue
{
    public partial class FrmChildren : Form
    {

        public string? Message { get; set; }
        public FrmChildren()
        {
            InitializeComponent();
            InitEvent();
        }

        public void InitEvent()
        {
            this.Load += FrmChildren_Load;
        }

        #region 父传子 相关

        private void FrmChildren_Load(object? sender, EventArgs e)
        {
            this.txtParent.Text = this.Message;
            var parentFrm = (FrmMain)this.Owner;
            parentFrm.DataTransfer += ParentFrm_DataTransfer; // 框架自带 EventHandler
            parentFrm.CustomParentTransferChildrenEvent += ParentFrm_CustomTransferData; // 自定义事件
        }

        /// <summary>
        /// 框架自带事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ParentFrm_DataTransfer(object? sender, DataEventArgs e)
        {
            this.txtParent.Text = e.Data;
        }

        /// <summary>
        /// 自定义事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ParentFrm_CustomTransferData(object sender, DataEventArgs e)
        {
            this.txtParent.Text = e.Data;
        }

        #endregion



        #region 子传递父 相关

        #region 子传递父 使用方法方案
        /// <summary>
        /// 子传父按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTransfer_Click(object sender, EventArgs e)
        {
            // 使用父窗体方法形式
            var parentFrm = (FrmMain)this.Owner;
            string childrenText = this.txtChildren.Text;
            parentFrm.SetChildrenText(childrenText);
        }

        #endregion


        #region 子传递父 委托事件方案

        public delegate void CustomChildrenTransferParent<T>(T data);
        public event CustomChildrenTransferParent<DataEventArgs> CustomChildrenTransferParentEvent;

        private void btn_Transfer_Value_Click(object sender, EventArgs e)
        {
            string input = this.txtChildren.Text;
            CustomChildrenTransferParentEvent(new DataEventArgs(input));
        }

        #endregion

        #endregion
    }
}
